package com.wq.concurrency.aqs;

/**
 * @Author: wangqiang20995
 * @Date:2019/7/24
 * @Description:
 * @Resource:
 */
public class ConditionObject {

    private static class Node {
        private int waitStatus;

        private Node nextWaiter;

        private Node prev;

        private Node next;

        private int value;

        public Node(int value) {
            this.value = value;
        }

        public Node() {
        }

        public void setWaitStatus(int status) {
            this.waitStatus = status;
        }

        @Override
        public String toString() {
            return "Node{" +
                    "waitStatus=" + waitStatus +
                    ", value=" + value +
                    '}';
        }
    }

    //Cancel
    public static final int CANCEL = 1;

    //signal
    public static final int SIGNAL = -1;


    private Node firstWaiter;

    private Node lastWaiter;

    public void clearCancelNode() {
        Node t = this.firstWaiter;
        Node trail = null;//这个指针的位置，总是在遍历的节点的前一个状态不为SIGNAL的节点，

        while (t != null) {
            Node next = t.nextWaiter;
            if (t.waitStatus != SIGNAL) {
                //node状态不是signal的，需要从链表中删除
                t.nextWaiter = null;
                if (trail == null) {
                    firstWaiter = next;//指向下一个节点
                } else {
                    trail.nextWaiter = next;//追踪指针的nextWaiter指向next
                }

                if (next == null) {
                    this.lastWaiter = trail;
                }
            } else {
                trail = t;
            }

            t = next;
        }
    }


    /**
     * @author:wangqiang20995
     * @datetime:2019/7/24 19:14
     * @param: [value]
     * @description:设置值为value的节点状态为cancel
     * @return: void
     **/
    public void cancelWait(int value) {

        if (this.firstWaiter == null) {
            return;
        }

        Node trail = this.firstWaiter;
        while (trail != null) {
            if (trail.value == value) {
                trail.waitStatus = CANCEL;
                break;
            }

            trail = trail.nextWaiter;
        }
    }

    /**
     * @author:wangqiang20995
     * @datetime:2019/7/24 19:15
     * @param: [value]
     * @description:将value封装为一个node，并且设置状态，将其加入到链表中
     * @return: void
     **/
    public void addConditionQueue(int value) {

        Node node = addNode(value);

        if (this.firstWaiter == null) {
            this.firstWaiter = node;
            this.lastWaiter = this.firstWaiter;
            return;
        }

        Node last = this.lastWaiter;
        last.nextWaiter = node;
        this.lastWaiter = node;
    }

    private Node addNode(int value) {
        Node node = new Node(value);
        node.waitStatus = SIGNAL;
        return node;
    }

    public static void main(String[] args) {
        ConditionObject conditionObject = new ConditionObject();
        conditionObject.addConditionQueue(1);
        conditionObject.addConditionQueue(2);
        conditionObject.addConditionQueue(3);
        conditionObject.addConditionQueue(4);
        conditionObject.addConditionQueue(5);

        //
        conditionObject.cancelWait(2);
        conditionObject.cancelWait(3);

        conditionObject.clearCancelNode();
        conditionObject.printLinkedList();

    }

    public void printLinkedList() {
        if (this.firstWaiter == null) {
            System.out.println("empty node list");
            return;
        }

        Node node = this.firstWaiter;
        while (node != null) {
            if (node.nextWaiter != null) {
                System.out.printf("[%s] -> ",node);
            } else {
                System.out.printf("[%s]\n",node);
            }

            node = node.nextWaiter;
        }
    }
}
